<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="客户编号" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入客户编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户性质" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择客户性质" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="headId">
        <el-input
          v-model="queryParams.headId"
          placeholder="请输入负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户级别" prop="rating">
        <el-select v-model="queryParams.rating" placeholder="请选择客户级别" clearable size="small">
          <el-option
            v-for="dict in ratingOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="企业类别" prop="entCate">
        <el-select v-model="queryParams.entCate" placeholder="请选择企业类别" clearable size="small">
          <el-option
            v-for="dict in entCateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有效" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择是否有效" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="会员卡号" prop="memberCard">
        <el-input
          v-model="queryParams.memberCard"
          placeholder="请输入会员卡号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="助记码" prop="supJm">
        <el-input
          v-model="queryParams.supJm"
          placeholder="请输入助记码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:customer:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crm:customer:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:customer:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crm:customer:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="助记码" align="center" prop="id" />
      <el-table-column label="客户编号" align="center" prop="sn" />
      <el-table-column label="客户性质" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="负责人" align="center" prop="headId" />
      <el-table-column label="客户名称" align="center" prop="name" />
      <el-table-column label="法人" align="center" prop="legalPerson" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="行业" align="center" prop="industry" :formatter="industryFormat" />
      <el-table-column label="客户级别" align="center" prop="rating" :formatter="ratingFormat" />
      <el-table-column label="企业类别" align="center" prop="entCate" :formatter="entCateFormat" />
      <el-table-column label="会员卡号" align="center" prop="memberCard" />
      <el-table-column label="助记码" align="center" prop="supJm" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crm:customer:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:customer:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="客户性质">
              <el-select v-model="form.type" placeholder="请选择客户性质" @change="typeChange">
                <el-option
                  v-for="dict in typeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户编号" prop="sn">
              <el-input v-model="form.sn" placeholder="系统自动生成" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="助记码" prop="supJm">
              <el-input v-model="form.supJm" placeholder="请输入助记码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入客户名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="会员卡号" prop="memberCard">
              <el-input v-model="form.memberCard" placeholder="请输入会员卡号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="金额" prop="amt">
              <el-input v-model="form.amt" placeholder="请输入金额" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="负责人" prop="headId">
              <el-input v-model="form.headId" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户来源">
              <el-select v-model="form.origin" placeholder="请选择客户来源">
                <el-option
                  v-for="dict in originOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="传真" prop="fax">
              <el-input v-model="form.fax" placeholder="请输入传真" :disabled="isDisable"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="电话" prop="telephone">
              <el-input v-model="form.telephone" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机" prop="mobile">
              <el-input v-model="form.mobile" placeholder="请输入手机" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="法人" prop="legalPerson" >
              <el-input v-model="form.legalPerson" placeholder="请输入法人" :disabled="isDisable"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮编" prop="zipCode">
              <el-input v-model="form.zipCode" placeholder="请输入邮编" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="行业" >
              <el-select v-model="form.industry" placeholder="请选择行业" :disabled="isDisable">
                <el-option
                  v-for="dict in industryOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业类别" >
              <el-select v-model="form.entCate" placeholder="请选择企业类别" :disabled="isDisable">
                <el-option
                  v-for="dict in entCateOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="年营业额" prop="yearTurnover" >
              <el-input v-model="form.yearTurnover" placeholder="请输入年营业额" :disabled="isDisable"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="与公司关系">
              <el-select v-model="form.ownership" placeholder="请选择与公司关系">
                <el-option
                  v-for="dict in ownershipOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="客户级别">
              <el-select v-model="form.rating" placeholder="请选择客户级别">
                <el-option
                  v-for="dict in ratingOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="人员规模" prop="staffSize" >
              <el-input v-model="form.staffSize" placeholder="请输入人员规模" :disabled="isDisable"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="公司阶段" >
              <el-select v-model="form.entStage" placeholder="请选择公司阶段" :disabled="isDisable">
                <el-option
                  v-for="dict in entStageOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营业执照" prop="businessLicense" >
              <el-input v-model="form.businessLicense" placeholder="请输入营业执照" :disabled="isDisable"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="注册资金" prop="registerCapital" >
              <el-input v-model="form.registerCapital" placeholder="请输入注册资金" :disabled="isDisable"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否有效">
              <el-select v-model="form.status" placeholder="请选择是否有效">
                <el-option
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="24">
            <el-form-item label="主营业务" prop="mainBusiness" >
              <el-input v-model="form.mainBusiness" type="textarea" placeholder="请输入内容" :disabled="isDisable"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomer, getCustomer, delCustomer, addCustomer, updateCustomer, exportCustomer } from "@/api/crm/customer";

export default {
  name: "Customer",
  data() {
    return {
      isDisable: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 客户信息表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 客户性质字典
      typeOptions: [],
      // 客户来源字典
      originOptions: [],
      // 行业字典
      industryOptions: [],
      // 与公司关系字典
      ownershipOptions: [],
      // 客户级别字典
      ratingOptions: [],
      // 是否删除字典
      isDeletedOptions: [],
      // 企业类别字典
      entCateOptions: [],
      // 公司阶段字典
      entStageOptions: [],
      // 是否有效字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sn: undefined,
        type: undefined,
        headId: undefined,
        name: undefined,
        rating: undefined,
        entCate: undefined,
        status: undefined,
        memberCard: undefined,
        supJm: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "客户性质不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("crm_customer_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("crm_customer_origin").then(response => {
      this.originOptions = response.data;
    });
    this.getDicts("crm_customer_industry").then(response => {
      this.industryOptions = response.data;
    });
    this.getDicts("crm_customer_ownership").then(response => {
      this.ownershipOptions = response.data;
    });
    this.getDicts("crm_customer_rating").then(response => {
      this.ratingOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isDeletedOptions = response.data;
    });
    this.getDicts("crm_customer_ent_cate").then(response => {
      this.entCateOptions = response.data;
    });
    this.getDicts("crm_customer_ent_stage").then(response => {
      this.entStageOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询客户信息列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 客户性质字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 客户来源字典翻译
    originFormat(row, column) {
      return this.selectDictLabel(this.originOptions, row.origin);
    },
    // 行业字典翻译
    industryFormat(row, column) {
      return this.selectDictLabel(this.industryOptions, row.industry);
    },
    // 与公司关系字典翻译
    ownershipFormat(row, column) {
      return this.selectDictLabel(this.ownershipOptions, row.ownership);
    },
    // 客户级别字典翻译
    ratingFormat(row, column) {
      return this.selectDictLabel(this.ratingOptions, row.rating);
    },
    // 是否删除字典翻译
    isDeletedFormat(row, column) {
      return this.selectDictLabel(this.isDeletedOptions, row.isDeleted);
    },
    // 企业类别字典翻译
    entCateFormat(row, column) {
      return this.selectDictLabel(this.entCateOptions, row.entCate);
    },
    // 公司阶段字典翻译
    entStageFormat(row, column) {
      return this.selectDictLabel(this.entStageOptions, row.entStage);
    },
    // 是否有效字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        companyId: undefined,
        sn: undefined,
        type: undefined,
        headId: undefined,
        createBy: undefined,
        name: undefined,
        legalPerson: undefined,
        origin: undefined,
        address: undefined,
        zipCode: undefined,
        industry: undefined,
        yearTurnover: undefined,
        ownership: undefined,
        rating: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        isDeleted: undefined,
        deleteBy: undefined,
        deleteDatetime: undefined,
        email: undefined,
        fax: undefined,
        telephone: undefined,
        mobile: undefined,
        entCate: undefined,
        staffSize: undefined,
        entStage: undefined,
        status: undefined,
        memberCard: undefined,
        amt: undefined,
        registerCapital: undefined,
        businessLicense: undefined,
        mainBusiness: undefined,
        supJm: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户信息";
      this.form.type = 1
      this.form.status = "Y"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCustomer(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCustomer(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除客户信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCustomer(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有客户信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCustomer(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    /** 客户性质 改变 **/
    typeChange(v){
     if(v === 1){
       this.isDisable = true
     }else {
       this.isDisable = false
     }
    }
  }
};
</script>
